package com.youlai.boot.modules.accounts.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.accounts.mapper.AccountOwnerMapper;
import com.youlai.boot.modules.accounts.service.AccountOwnerService;
import com.youlai.boot.modules.accounts.model.entity.AccountOwner;
import com.youlai.boot.modules.accounts.model.form.AccountOwnerForm;
import com.youlai.boot.modules.accounts.model.query.AccountOwnerQuery;
import com.youlai.boot.modules.accounts.model.vo.AccountOwnerVO;
import com.youlai.boot.modules.accounts.converter.AccountOwnerConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 账户归属人服务实现类
 *
 * @author lz0816
 * @since 2025-05-23 15:10
 */
@Service
@RequiredArgsConstructor
public class AccountOwnerServiceImpl extends ServiceImpl<AccountOwnerMapper, AccountOwner> implements AccountOwnerService {

    private final AccountOwnerConverter accountOwnerConverter;

    /**
    * 获取账户归属人分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<AccountOwnerVO>} 账户归属人分页列表
    */
    @Override
    public IPage<AccountOwnerVO> getAccountOwnerPage(AccountOwnerQuery queryParams) {
        Page<AccountOwnerVO> pageVO = this.baseMapper.getAccountOwnerPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取账户归属人表单数据
     *
     * @param id 账户归属人ID
     * @return
     */
    @Override
    public AccountOwnerForm getAccountOwnerFormData(Long id) {
        AccountOwner entity = this.getById(id);
        return accountOwnerConverter.toForm(entity);
    }
    
    /**
     * 新增账户归属人
     *
     * @param formData 账户归属人表单对象
     * @return
     */
    @Override
    public boolean saveAccountOwner(AccountOwnerForm formData) {
        AccountOwner entity = accountOwnerConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新账户归属人
     *
     * @param id   账户归属人ID
     * @param formData 账户归属人表单对象
     * @return
     */
    @Override
    public boolean updateAccountOwner(Long id,AccountOwnerForm formData) {
        AccountOwner entity = accountOwnerConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除账户归属人
     *
     * @param ids 账户归属人ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteAccountOwners(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的账户归属人数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
